package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.CommodityType;
import com.erp.bean.Supplier;
import com.erp.service.CommodityService;
import com.erp.service.CommodityTypeService;
import com.erp.service.SupplierService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.CommodityTypeServiceImpl;
import com.erp.service.impl.SupplierServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 * @author ld
 * 商品添加控制器
 */
@WebServlet("/commodityadd.action")
public class CommodityAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑对象
        CommodityService commodityService = new CommodityServiceImpl();
        //创建供应商业务逻辑对象
        SupplierService supplierService = new SupplierServiceImpl();
        //创建商品类业务逻辑对象
        CommodityTypeService commodityTypeService = new CommodityTypeServiceImpl();
        //查询供应商信息
        List<Supplier> list = supplierService.findAll();
        System.out.println(list);
        //查询商品类型信息
        List<CommodityType> list1 = commodityTypeService.findAll();
        System.out.println(list1);
        String type = req.getParameter("type");
        if (type.equals("1")) {
            req.setAttribute("list", list);
            req.setAttribute("list1", list1);
            //转发到商品添加页面
            req.getRequestDispatcher("commodityadd.jsp").forward(req, resp);
        } else if (type.equals("2")) {
            //接收商品编号、供应商、商品类型
            //接收用户参数
            String strgname = req.getParameter("gname");//获取商品名称
            System.out.println(strgname);
            String price1 = req.getParameter("price");//获取商品价格
            //将字符串转化为double类型
            double price = Double.valueOf(price1);
            System.out.println(price);
            String model = req.getParameter("model");//获取系统版本
            System.out.println(model);
            String prddate1 = req.getParameter("prddate");//获取商品生产日期
            //将字符串转化为Date类型
            Date prddate = Date.valueOf(prddate1);
            System.out.println(prddate);
            String supplier1 = req.getParameter("supplier1");//获取供应商
            //将字符串转化为int类型
            int supplier = Integer.valueOf(supplier1);
            System.out.println(supplier);
            String type1 = req.getParameter("itype");//获取商品类型
            //将字符串转化为int类型
            int types = Integer.valueOf(type1);
            System.out.println(types);
            //创建商品对象
            Commodity commodity = new Commodity();
            commodity.setGname(strgname);//设置名称
            commodity.setPrice(price);//设置价格
            commodity.setModel(model);//设置版本
            commodity.setPrddate(prddate);//设置生产日期
            commodity.setSupid(supplier);//设置供应商
            commodity.setTyid(types);//设置类型
            boolean flag = commodityService.add(commodity);
            System.out.println(flag);//判断是否成功
            //重新定向到商品列表控制器
            resp.sendRedirect("commoditylist.action");

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
